{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 一.原理介绍\n",
    "主成分分析想做的事如下图所示，将原始坐标系$<w_1,w_2>$变换为新的坐标系$<w'_1,w'_2>$，是的原始数据在新的坐标系下具有如下特点：   \n",
    "\n",
    "（1）在第一个坐标轴上的投影分散的尽可能的开（第一主成分），在第二个坐标轴上的投影其次（第二主成分）....  \n",
    "\n",
    "（2）新的坐标轴彼此正交，这样可以去掉变量间的相关性，如下面第一张图中数据几乎排在$y=x$这条线上，而经过pca的变换后，数据各维度失去了相关性\n",
    "![avatar](./source/19_pca1.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 二.求解第一个坐标轴（第一主成分）\n",
    "\n",
    "接下来让我们先从第一个坐标轴入手，根据我们的目标，我们要让原始的数据点$X\\in R^{m\\times n}$在某一个方向$w\\in R^n$上的投影点尽可能的分散，这里涉及两个点：   \n",
    "\n",
    "（1）如果计算投影坐标；   \n",
    "\n",
    "（2）如何衡量分散程度；   \n",
    "\n",
    "下面依次推导\n",
    "#### 投影坐标的计算\n",
    "\n",
    "抽样坐标的计算推导如下图，将某样本点$x$看做一个向量，将坐标轴方向向量$w$移动到与$x$相同的原点，假设两向量的夹角为$\\theta$，在$w^Tw=1$的约束条件下，经过推导可得投影坐标可表示为$x^Tw$\n",
    "![avatar](./source/19_pca2.png)\n",
    "\n",
    "#### 分散程度的衡量：方差\n",
    "\n",
    "对于分散程度，显然最直接的一个参考指标可以使用方差，方差越大说明数据越分散，那么在坐标轴$w$上所有数据投影点的方差如下：  \n",
    "\n",
    "$$\n",
    "S(X,w)=\\sum_{i=1}^m(x_i^Tw-\\bar{x}^Tw)^2\n",
    "$$  \n",
    "\n",
    "这里，$x_i\\in R^n$，$\\bar{x}=\\frac{\\sum_{i=1}^m x_i}{m}$，通常为了方便可以先进行**去中心化**的操作，即令$X$中的样本点都减去它每个维度的均值\n",
    "\n",
    "$$\n",
    "X=X-\\bar{X}\n",
    "$$  \n",
    "\n",
    "所以这时的方差就可以简单写作：   \n",
    "\n",
    "$$\n",
    "S(X,w)=\\sum_{i=1}^m(x_i^Tw)^2=\\sum_{i=1}^m w^Tx_ix_i^Tw=w^T\\sum_{i=1}^m (x_ix_i^T)w=w^T[x_1,x_2,...,x_m]\\cdot\\begin{bmatrix}\n",
    "x_1^T\\\\ \n",
    "x_2^T\\\\ \n",
    "...\\\\ \n",
    "x_m^T\n",
    "\\end{bmatrix}w=w^TX^TXw\n",
    "$$  \n",
    "\n",
    "所以，我们我们要求最优$w^*$便是  \n",
    "\n",
    "$$\n",
    "w^*=arg\\max_{w}w^TX^TXw\n",
    "$$  \n",
    "\n",
    "这依赖于$X^TX$的性质"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 三.基于$X^TX$求解前$k$个主成分\n",
    "\n",
    "由于$X^TX$为实对称矩阵，我们可以对其进行正交分解，且其特征值均大于等于0，所以我们可以将$X^TX$分解为如下形式：   \n",
    "\n",
    "$$\n",
    "X^TX=U\\Lambda U^T=[u_1,u_2,...,u_n]\\begin{bmatrix}\n",
    "\\lambda_1 & 0 & 0 & 0 \\\\ \n",
    "0 & \\lambda_2 & 0 & 0\\\\ \n",
    "0 & 0 & \\ddots & 0\\\\ \n",
    "0 & 0 & 0 & \\lambda_n\n",
    "\\end{bmatrix}\\begin{bmatrix}\n",
    "u_1^T\\\\ \n",
    "u_2^T\\\\ \n",
    "...\\\\ \n",
    "u_n^T\n",
    "\\end{bmatrix}\n",
    "$$  \n",
    "\n",
    "其中，$\\lambda_1\\geq\\lambda_2\\geq\\cdots\\geq\\lambda_n\\geq 0,u_i^Tu_j=\\left\\{\\begin{matrix}\n",
    "1 &i=j \\\\ \n",
    "0 &i\\neq j\n",
    "\\end{matrix}\\right.\n",
    "$\n",
    "\n",
    "对于，任意$w(s.t. w^Tw=1)$，都有：   \n",
    "\n",
    "$$\n",
    "w^TX^TXw=(w^Tu_1)^2\\lambda_1+(w^Tu_2)^2\\lambda_2+\\cdots+(w^Tu_n)^2\\lambda_n\n",
    "$$  \n",
    "\n",
    "\n",
    "由于$u_1,u_2,...,u_n$彼此正交，且为单位向量，所以$(w^Tu_1)^2+(w^Tu_2)^2+\\cdots+(w^Tu_n)^2=1$，所以结果也就可以看出来了：   \n",
    "\n",
    "（1）对于第一个坐标轴（第一主成分），即是$\\lambda_1$对应的特征向量$u_1$...\n",
    "\n",
    "（2）对于第二个坐标轴（第二主成分），即是$\\lambda_2$对应的特征向量$u_2$...\n",
    "\n",
    "（3）......  \n",
    "\n",
    "通常，我们选择前$k$个坐标轴进行映射$U_k=[u_1,u_2,...,u_k]$，生成的新数据为$X_k=XU_k$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 四.代码实现\n",
    "\n",
    "#### 造伪数据\n",
    "将数据点$(x,y)$分布在$y=3x+2$上下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "#造伪样本\n",
    "X=np.linspace(0,10,100)\n",
    "Y=3*X+2\n",
    "X+=np.random.normal(size=(X.shape))*0.3#添加噪声\n",
    "data=np.c_[X,Y]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0,0.5,'y')"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAEKCAYAAAAfGVI8AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAG3dJREFUeJzt3X2MXOV1x/Hf8XiAsZN2QSzUbOKapsi8BNlbthGNpSo4L04TBBtooJFKaBXVVRUaQJZbJ6q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pmSzpC01Mz+Mt9RFV+ZA8GLkt7e8vXbVJAlaDszq6oeBO5w93vzHk8G1ki6xMx+qvoW31oz+1q+Q8rEi5JedPfmiu5u1QNDkbxP0nPuftDdZyTdK+ndOY8pSy+b2TJJanx8JY9BlDkQ/EDSWWZ2ppmdoPqB1P05jyl1Zmaq7ynvc/cv5D2eLLj7p939be6+QvX/H3e6e+Fmke7+c0k/M7OVjYfeK+lHOQ4pCy9IutDMljT+232vCnYg3uZ+SVc3Pr9a0n15DKLwdxaHcfejZnaNpB2qZybc5u57cx5WFtZIukrSU2a2p/HYZ9z92zmOCd37e0l3NCYvP5H01zmPJ1Xu/riZ3S3pCdUz3ibVJ9W3C2Vmd0p6j6RTzexFSTdI2iJpu5l9QvUg+NFcxkZlMQCUW5m3hgAAIhAAQOkRCACg5AgEAFByBAIAKDkCAQCUHIEAAEqOQAB0wcz+2Mx+aGYnmdnSRv/8d+Y9LqAbFJQBXTKzz0o6SVJN9R5AN+U8JKArBAKgS402Dz+Q9Lqkd7v7bM5DArrC1hDQvVMkvUXSW1VfGQADiRUB0CUzu1/1ttdnSlrm7tfkPCSgK6XtPgoshJl9XNJRd/964/7r/zGzte6+M++xAZ1iRQAAJccZAQCUHIEAAEqOQAAAJUcgAICSIxAAQMkRCACg5AgEAFByBAIAKLn/B6VOJYmX3UjgAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "plt.scatter(data[:,0],data[:,1])\n",
    "plt.xlabel('x')\n",
    "plt.ylabel('y')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 去中心化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "data=data-np.mean(data,axis=0,keepdims=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 求$X^TX$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "xTx=data.T.dot(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 正交分解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "eig_vals,eig_vecs=np.linalg.eig(xTx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8.27256772e+00, 8.50444256e+03])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eig_vals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.94847792, -0.31684323],\n",
       "       [ 0.31684323, -0.94847792]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eig_vecs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "校验一下特征分解结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.25055521e-12,  0.00000000e+00],\n",
       "       [ 0.00000000e+00, -9.09494702e-13]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eig_vecs.dot(np.diag(eig_vals)).dot(eig_vecs.T)-xTx"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "由于`np.linalg.eig`的特征值并未按从大到小的顺序排序，所以我们需要进行一下重排"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "sorted_indice=np.argsort(-1*eig_vals)#默认是升序排序\n",
    "eig_vals=eig_vals[sorted_indice]\n",
    "eig_vecs[:]=eig_vecs[:,sorted_indice]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在的特征矩阵，第一列便是第一主成分，第二列便是第二主成分，接下来我们可以轻易拿到转换后的数据，只需要去中心化数据矩阵和特征矩阵相乘即可"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_data=data.dot(eig_vecs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 变化后的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-20, 20)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY0AAAEKCAYAAADuEgmxAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAFbNJREFUeJzt3X2QXXV9x/HPJ5sLbIBOlrJKXEgJDkKJliC3VEt1FBEQHwhQDIy1cXQm2pGp/lFrKB1Nh1qiFGJnOlVjYWQ6qGWEQEZbeVAqU1oqG8JDQoiEBzUPkkWM2BLjJvn2j3tuuLt77t3f3XvvnrPJ+zWzs+eehz3f+9tzzuc83uuIEAAAKWYVXQAAYOYgNAAAyQgNAEAyQgMAkIzQAAAkIzQAAMkKCw3bJ9i+z/Ym2xttfyLrf4zte2w/lf0eKKpGAMBYLuo5DdvzJM2LiIdtHy1pnaTFkj4k6cWIWGl7uaSBiPh0IUUCAMYo7EgjInZExMNZ968kbZI0JOkiSTdno92sWpAAAEqgsCONMUXYJ0q6X9LrJf0kIuY2DPtFREw4RWV7maRlknTkkUeeeeqpp05PsQBwkFi3bt0LETHYz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bVp0Vf0O7wbYItq540fyX6+3DDsatXuXtks6V0F13mxanueeyQ9L+murP+lkjaqdmfNw5LeW8Y6y9ae42r+F0mPS3osW/jnFV1TQ20XqnaHytOqnf4rvKacGk/Klr9Hs2WxNHVK+oZqp3BHs+XyI5J+W9L3JD2V/T6mpHWWbrmU9EeqnS57rGGbeWG7bcrHiAAAks3o01MAgOlFaAAAkhEaAIBkhAYAIBmhAQBIRmgAAJIRGgCAZIQG0AO2fz/7sLojsqeuN9p+fdF1AZ3i4T6gR2z/raQjJPVL2hoR1xZcEtAxQgPokewzpx6S9GtJfxgR+wouCegYp6eA3jlG0lGqfUvaEQXXAnQFRxpAj9heq9o39i1Q7QPrriy4JKBjpf8+DWAmsv2nkvZGxNezj7//L9vnRMT3i64N6ARHGgCAZFzTAAAkIzQAAMkIDQBAMkIDAJCM0AAAJCM0AADJCA0AQLL/B7+2QFga/XqmAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(new_data[:,0],new_data[:,1])\n",
    "plt.xlabel('x')\n",
    "plt.ylabel('y')\n",
    "plt.xlim(-20, 20)\n",
    "plt.ylim(-20, 20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以发现，最终得到了我们想要的结果...，最后将代码整理一下，放到`ml_models.decomposition.PCA`中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
